{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8d216c55",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:37:51.985919Z",
     "start_time": "2022-11-21T13:37:50.352290Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aeeb0a96",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:38:41.455561Z",
     "start_time": "2022-11-21T13:38:41.429629Z"
    }
   },
   "outputs": [],
   "source": [
    "data = pd.read_csv('./dataset/credit-a.csv', header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9758dd85",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:38:43.855139Z",
     "start_time": "2022-11-21T13:38:43.778345Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "      <th>15</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>30.83</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1.25</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>202</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>58.67</td>\n",
       "      <td>4.460</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>3.04</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>43</td>\n",
       "      <td>560.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>24.50</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>1.50</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>280</td>\n",
       "      <td>824.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>27.83</td>\n",
       "      <td>1.540</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>3.75</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>3.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>20.17</td>\n",
       "      <td>5.625</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1.71</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>120</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>648</th>\n",
       "      <td>0</td>\n",
       "      <td>21.08</td>\n",
       "      <td>10.085</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>1.25</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>260</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>649</th>\n",
       "      <td>1</td>\n",
       "      <td>22.67</td>\n",
       "      <td>0.750</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>200</td>\n",
       "      <td>394.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>650</th>\n",
       "      <td>1</td>\n",
       "      <td>25.25</td>\n",
       "      <td>13.500</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>7</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>200</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>651</th>\n",
       "      <td>0</td>\n",
       "      <td>17.92</td>\n",
       "      <td>0.205</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0.04</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>280</td>\n",
       "      <td>750.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>0</td>\n",
       "      <td>35.00</td>\n",
       "      <td>3.375</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8.29</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>653 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     0      1       2   3   4   5   6     7   8   9   10  11  12   13     14  \\\n",
       "0     0  30.83   0.000   0   0   9   0  1.25   0   0   1   1   0  202    0.0   \n",
       "1     1  58.67   4.460   0   0   8   1  3.04   0   0   6   1   0   43  560.0   \n",
       "2     1  24.50   0.500   0   0   8   1  1.50   0   1   0   1   0  280  824.0   \n",
       "3     0  27.83   1.540   0   0   9   0  3.75   0   0   5   0   0  100    3.0   \n",
       "4     0  20.17   5.625   0   0   9   0  1.71   0   1   0   1   2  120    0.0   \n",
       "..   ..    ...     ...  ..  ..  ..  ..   ...  ..  ..  ..  ..  ..  ...    ...   \n",
       "648   0  21.08  10.085   1   1  11   1  1.25   1   1   0   1   0  260    0.0   \n",
       "649   1  22.67   0.750   0   0   0   0  2.00   1   0   2   0   0  200  394.0   \n",
       "650   1  25.25  13.500   1   1  13   7  2.00   1   0   1   0   0  200    1.0   \n",
       "651   0  17.92   0.205   0   0  12   0  0.04   1   1   0   1   0  280  750.0   \n",
       "652   0  35.00   3.375   0   0   0   1  8.29   1   1   0   0   0    0    0.0   \n",
       "\n",
       "     15  \n",
       "0    -1  \n",
       "1    -1  \n",
       "2    -1  \n",
       "3    -1  \n",
       "4    -1  \n",
       "..   ..  \n",
       "648   1  \n",
       "649   1  \n",
       "650   1  \n",
       "651   1  \n",
       "652   1  \n",
       "\n",
       "[653 rows x 16 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "621f4494",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:39:08.921073Z",
     "start_time": "2022-11-21T13:39:08.910103Z"
    }
   },
   "outputs": [],
   "source": [
    "X = data.iloc[:, :-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c73dc42",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:39:12.973231Z",
     "start_time": "2022-11-21T13:39:12.943312Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>30.83</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1.25</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>202</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>58.67</td>\n",
       "      <td>4.460</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>3.04</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>43</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>24.50</td>\n",
       "      <td>0.500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>1.50</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>280</td>\n",
       "      <td>824.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>27.83</td>\n",
       "      <td>1.540</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>3.75</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>100</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>20.17</td>\n",
       "      <td>5.625</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1.71</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>120</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>648</th>\n",
       "      <td>0</td>\n",
       "      <td>21.08</td>\n",
       "      <td>10.085</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>1.25</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>260</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>649</th>\n",
       "      <td>1</td>\n",
       "      <td>22.67</td>\n",
       "      <td>0.750</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>200</td>\n",
       "      <td>394.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>650</th>\n",
       "      <td>1</td>\n",
       "      <td>25.25</td>\n",
       "      <td>13.500</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>7</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>200</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>651</th>\n",
       "      <td>0</td>\n",
       "      <td>17.92</td>\n",
       "      <td>0.205</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0.04</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>280</td>\n",
       "      <td>750.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>652</th>\n",
       "      <td>0</td>\n",
       "      <td>35.00</td>\n",
       "      <td>3.375</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8.29</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>653 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     0      1       2   3   4   5   6     7   8   9   10  11  12   13     14\n",
       "0     0  30.83   0.000   0   0   9   0  1.25   0   0   1   1   0  202    0.0\n",
       "1     1  58.67   4.460   0   0   8   1  3.04   0   0   6   1   0   43  560.0\n",
       "2     1  24.50   0.500   0   0   8   1  1.50   0   1   0   1   0  280  824.0\n",
       "3     0  27.83   1.540   0   0   9   0  3.75   0   0   5   0   0  100    3.0\n",
       "4     0  20.17   5.625   0   0   9   0  1.71   0   1   0   1   2  120    0.0\n",
       "..   ..    ...     ...  ..  ..  ..  ..   ...  ..  ..  ..  ..  ..  ...    ...\n",
       "648   0  21.08  10.085   1   1  11   1  1.25   1   1   0   1   0  260    0.0\n",
       "649   1  22.67   0.750   0   0   0   0  2.00   1   0   2   0   0  200  394.0\n",
       "650   1  25.25  13.500   1   1  13   7  2.00   1   0   1   0   0  200    1.0\n",
       "651   0  17.92   0.205   0   0  12   0  0.04   1   1   0   1   0  280  750.0\n",
       "652   0  35.00   3.375   0   0   0   1  8.29   1   1   0   0   0    0    0.0\n",
       "\n",
       "[653 rows x 15 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a2b62eb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:39:29.070163Z",
     "start_time": "2022-11-21T13:39:29.062185Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     -1\n",
       "1     -1\n",
       "2     -1\n",
       "3     -1\n",
       "4     -1\n",
       "      ..\n",
       "648    1\n",
       "649    1\n",
       "650    1\n",
       "651    1\n",
       "652    1\n",
       "Name: 15, Length: 653, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = data.iloc[:, -1]\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1478ad48",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:39:51.843232Z",
     "start_time": "2022-11-21T13:39:51.798352Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 653 entries, 0 to 652\n",
      "Data columns (total 16 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   0       653 non-null    int64  \n",
      " 1   1       653 non-null    float64\n",
      " 2   2       653 non-null    float64\n",
      " 3   3       653 non-null    int64  \n",
      " 4   4       653 non-null    int64  \n",
      " 5   5       653 non-null    int64  \n",
      " 6   6       653 non-null    int64  \n",
      " 7   7       653 non-null    float64\n",
      " 8   8       653 non-null    int64  \n",
      " 9   9       653 non-null    int64  \n",
      " 10  10      653 non-null    int64  \n",
      " 11  11      653 non-null    int64  \n",
      " 12  12      653 non-null    int64  \n",
      " 13  13      653 non-null    int64  \n",
      " 14  14      653 non-null    float64\n",
      " 15  15      653 non-null    int64  \n",
      "dtypes: float64(4), int64(12)\n",
      "memory usage: 81.8 KB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f626f8d4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:40:50.640433Z",
     "start_time": "2022-11-21T13:40:50.622481Z"
    }
   },
   "outputs": [],
   "source": [
    "# 把-1改成0, 表示负样本. \n",
    "Y.replace(-1, 0, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1d86eb3a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:40:56.714182Z",
     "start_time": "2022-11-21T13:40:56.703214Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1], dtype=int64)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "97b7deb8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:41:07.204116Z",
     "start_time": "2022-11-21T13:41:07.149262Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    357\n",
       "0    296\n",
       "Name: 15, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6fa6f68d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:42:31.485125Z",
     "start_time": "2022-11-21T13:42:31.470165Z"
    }
   },
   "outputs": [],
   "source": [
    "# 变成tensor\n",
    "X = torch.from_numpy(X.values).type(torch.FloatTensor)\n",
    "Y = torch.from_numpy(Y.values.reshape(-1, 1)).type(torch.FloatTensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "842c5e93",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:43:48.202861Z",
     "start_time": "2022-11-21T13:43:48.190893Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([653, 15])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "23195cfa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:43:02.574941Z",
     "start_time": "2022-11-21T13:43:02.557987Z"
    }
   },
   "outputs": [],
   "source": [
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f839b207",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:44:34.699455Z",
     "start_time": "2022-11-21T13:44:34.693471Z"
    }
   },
   "outputs": [],
   "source": [
    "model = nn.Sequential(\n",
    "    nn.Linear(15, 512),\n",
    "    nn.Linear(512, 1),\n",
    "    nn.Sigmoid()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d8fdf532",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:44:38.705736Z",
     "start_time": "2022-11-21T13:44:38.692771Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Sequential(\n",
       "  (0): Linear(in_features=15, out_features=512, bias=True)\n",
       "  (1): Linear(in_features=512, out_features=1, bias=True)\n",
       "  (2): Sigmoid()\n",
       ")"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ed240703",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:45:16.076747Z",
     "start_time": "2022-11-21T13:45:16.062784Z"
    }
   },
   "outputs": [],
   "source": [
    "# 二分类交叉熵\n",
    "loss_fn = nn.BCELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3ffeee8c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:45:44.050901Z",
     "start_time": "2022-11-21T13:45:44.045914Z"
    }
   },
   "outputs": [],
   "source": [
    "optimizer = torch.optim.SGD(model.parameters(), lr=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "eeb4a029",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:46:32.082388Z",
     "start_time": "2022-11-21T13:46:32.077401Z"
    }
   },
   "outputs": [],
   "source": [
    "# 正常神经网络训练是一个batch一个batch\n",
    "batch_size = 32\n",
    "steps = 653 // 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3e45c658",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:49:30.532442Z",
     "start_time": "2022-11-21T13:49:21.853662Z"
    }
   },
   "outputs": [],
   "source": [
    "for epoch in range(1000):\n",
    "    for batch in range(steps):\n",
    "        start = batch * batch_size\n",
    "        end = start + batch_size\n",
    "        x = X[start:end]\n",
    "        y = Y[start:end]\n",
    "        y_pred = model(x)\n",
    "        loss = loss_fn(y_pred, y)\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "77ea5084",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:50:03.263866Z",
     "start_time": "2022-11-21T13:50:03.228960Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('0.weight',\n",
       "              tensor([[-0.2510, -0.0917, -0.1623,  ...,  0.0688,  0.0101,  0.2397],\n",
       "                      [-0.0924, -0.0720,  0.2507,  ...,  0.0427,  0.1288,  0.0303],\n",
       "                      [ 0.0991, -0.0459, -0.0488,  ..., -0.2309, -0.0752, -0.0264],\n",
       "                      ...,\n",
       "                      [-0.2204,  0.0958,  0.0440,  ...,  0.2151,  0.1661, -0.2102],\n",
       "                      [-0.1011,  0.1929, -0.0635,  ...,  0.1204,  0.1412,  0.1342],\n",
       "                      [-0.2260,  0.0124,  0.1983,  ...,  0.1256, -0.2107,  0.2492]])),\n",
       "             ('0.bias',\n",
       "              tensor([-0.1556,  0.0715, -0.2211,  0.1314,  0.0178,  0.1937,  0.0756,  0.1372,\n",
       "                       0.1291,  0.1450, -0.1565,  0.2017, -0.2572,  0.0980, -0.0820,  0.0146,\n",
       "                       0.1368,  0.1496,  0.1936,  0.0957, -0.1675,  0.2450,  0.1044, -0.1162,\n",
       "                       0.1013,  0.0354,  0.0818,  0.0776, -0.0998,  0.2017, -0.2064,  0.0156,\n",
       "                       0.2510, -0.0912,  0.2437, -0.0412, -0.1486,  0.2155,  0.1209,  0.1435,\n",
       "                      -0.2296, -0.2355,  0.0749, -0.1362,  0.1078, -0.2564, -0.1881, -0.1981,\n",
       "                      -0.0596, -0.2496, -0.2322,  0.1561,  0.0660, -0.0491, -0.1233, -0.0739,\n",
       "                       0.0263, -0.2015,  0.0341,  0.1893,  0.2120, -0.2186, -0.1451, -0.2102,\n",
       "                       0.2171, -0.0198,  0.2089, -0.2129, -0.1528, -0.1707,  0.2482, -0.2008,\n",
       "                       0.1532,  0.0102, -0.2124,  0.1358,  0.1711, -0.1868,  0.0316, -0.0035,\n",
       "                       0.0121,  0.1551, -0.2133, -0.2326, -0.0953, -0.1276,  0.0737,  0.2234,\n",
       "                      -0.1305, -0.1546,  0.0362,  0.0603,  0.1579,  0.1785, -0.1388,  0.0031,\n",
       "                       0.2018, -0.0764, -0.0731, -0.1081, -0.0137, -0.0256, -0.2438,  0.1112,\n",
       "                      -0.1985, -0.1723,  0.0048,  0.1316, -0.1883,  0.1077,  0.0303,  0.1312,\n",
       "                      -0.1674,  0.1762,  0.0103, -0.2131,  0.0416, -0.2076, -0.0287,  0.1899,\n",
       "                      -0.1569, -0.1052,  0.0471, -0.0977, -0.0325, -0.2236,  0.1691, -0.1041,\n",
       "                      -0.2024, -0.1001,  0.1580, -0.0125,  0.1706,  0.0297,  0.1797, -0.0020,\n",
       "                      -0.2569, -0.1412, -0.1825,  0.0192, -0.0825, -0.0423, -0.0131, -0.1995,\n",
       "                       0.1629, -0.1770,  0.1036, -0.0263, -0.0438,  0.1247, -0.2492,  0.2576,\n",
       "                      -0.2079, -0.1802,  0.1405,  0.1420,  0.0053, -0.2172,  0.0392, -0.1226,\n",
       "                      -0.1570,  0.0640,  0.1269, -0.2528, -0.0816,  0.0743, -0.0414,  0.1285,\n",
       "                       0.2297,  0.2348,  0.0894,  0.2062, -0.0356,  0.0397,  0.2174,  0.1532,\n",
       "                      -0.1838,  0.0779, -0.1279,  0.1484, -0.1217,  0.0546,  0.1649,  0.2356,\n",
       "                       0.1765,  0.1363,  0.2420,  0.0046, -0.2578, -0.1580,  0.2216, -0.1656,\n",
       "                      -0.1401,  0.0295, -0.1146,  0.0556, -0.0932, -0.1441, -0.1200, -0.2074,\n",
       "                      -0.1664,  0.2251,  0.1339,  0.0132, -0.0230,  0.0518,  0.0630,  0.0018,\n",
       "                       0.1792,  0.1342,  0.0400,  0.0334,  0.1824, -0.1580,  0.0215, -0.1594,\n",
       "                      -0.1627, -0.2257, -0.0458, -0.2495,  0.1979, -0.1961, -0.0088, -0.1686,\n",
       "                       0.1758, -0.1527, -0.2157,  0.0159, -0.2229,  0.0107,  0.1160, -0.2066,\n",
       "                      -0.1079,  0.0648, -0.0723, -0.2551, -0.2421,  0.1026, -0.2414, -0.1792,\n",
       "                       0.0407, -0.1209,  0.2287, -0.0871, -0.1315, -0.0820,  0.1766,  0.2410,\n",
       "                       0.1856,  0.2304,  0.0710, -0.0439, -0.1038,  0.1470, -0.0279, -0.1436,\n",
       "                      -0.0262, -0.1343,  0.2326, -0.2530,  0.1639,  0.0301, -0.1151,  0.1088,\n",
       "                      -0.1434,  0.1133, -0.1517,  0.0103,  0.1451, -0.2486,  0.0207, -0.0434,\n",
       "                      -0.2334,  0.2303, -0.0783,  0.0622, -0.0864,  0.1971, -0.0983,  0.2185,\n",
       "                       0.0020,  0.0141,  0.0018,  0.2387, -0.0025,  0.1226,  0.0648,  0.0257,\n",
       "                       0.0834, -0.2299, -0.2540, -0.0349, -0.2175,  0.1844,  0.1803, -0.2404,\n",
       "                       0.1554,  0.1074,  0.1297,  0.1898,  0.2075,  0.1601,  0.2100, -0.1027,\n",
       "                       0.1462,  0.1250,  0.1161,  0.0827,  0.0589,  0.1950,  0.1777,  0.0794,\n",
       "                      -0.0487,  0.0697, -0.1590,  0.1050, -0.0943, -0.0504,  0.1564, -0.1911,\n",
       "                      -0.2533, -0.0005,  0.0412, -0.1670, -0.1210, -0.0929,  0.1284, -0.0151,\n",
       "                      -0.2015, -0.0989,  0.1438,  0.1829, -0.1608, -0.1690, -0.2143,  0.0063,\n",
       "                      -0.0774,  0.1273, -0.0085, -0.0346, -0.1018, -0.0510,  0.0701, -0.1448,\n",
       "                       0.1092,  0.0937,  0.2568, -0.0506,  0.2185, -0.0113,  0.1859,  0.0977,\n",
       "                       0.1512,  0.2267, -0.1821,  0.1122, -0.1870, -0.0105, -0.2219,  0.0251,\n",
       "                       0.2193, -0.0980, -0.0792,  0.1270,  0.0689,  0.1922,  0.2055, -0.1585,\n",
       "                       0.2375,  0.1017, -0.2124, -0.0458, -0.0774,  0.0222,  0.0707, -0.0625,\n",
       "                       0.0598,  0.0857, -0.1649,  0.1786, -0.0452,  0.0925,  0.1945, -0.1286,\n",
       "                       0.1857,  0.2460, -0.1327,  0.0871, -0.2522,  0.2477,  0.1509,  0.2260,\n",
       "                       0.0887,  0.1846,  0.0284,  0.1028,  0.1450, -0.0236,  0.0192, -0.0207,\n",
       "                       0.1511, -0.0737,  0.1571, -0.1913, -0.2111, -0.0788, -0.1110,  0.0841,\n",
       "                       0.2331, -0.2508,  0.0347, -0.0741,  0.0594, -0.0125, -0.0175,  0.2464,\n",
       "                       0.0493, -0.1436,  0.0337, -0.1012, -0.1427,  0.0412, -0.1395,  0.1974,\n",
       "                       0.1851, -0.0427, -0.0028,  0.0166,  0.1382, -0.1623, -0.2149, -0.0957,\n",
       "                       0.1701,  0.2228, -0.1183,  0.1708,  0.1407,  0.0174, -0.2283,  0.1348,\n",
       "                      -0.2519,  0.1457,  0.1932, -0.0444,  0.1960, -0.0316,  0.0988,  0.0556,\n",
       "                      -0.1294, -0.1708, -0.1860,  0.2508, -0.2273,  0.0450, -0.0205, -0.1538,\n",
       "                       0.0340, -0.0216,  0.0248, -0.1346,  0.0047, -0.2382,  0.1164,  0.1971,\n",
       "                      -0.1931, -0.0145, -0.1169, -0.0155,  0.2402,  0.1722,  0.1659, -0.0929,\n",
       "                       0.0129, -0.1289,  0.0594,  0.0065, -0.0471,  0.0841,  0.1463,  0.2259,\n",
       "                      -0.1417, -0.1286,  0.1877,  0.2311,  0.1494, -0.0054, -0.0491,  0.2184,\n",
       "                      -0.1405,  0.0295, -0.0219,  0.0952,  0.2028,  0.0883,  0.2369, -0.2484,\n",
       "                       0.0090, -0.0538,  0.1737, -0.2186,  0.1295, -0.1941, -0.1040,  0.1776,\n",
       "                      -0.2120,  0.2188, -0.1571, -0.0860,  0.2008, -0.2343, -0.2018, -0.0561])),\n",
       "             ('1.weight',\n",
       "              tensor([[ 4.1688e-02, -1.9799e-02, -3.4119e-02, -2.1652e-02, -9.6831e-02,\n",
       "                        7.3192e-02, -1.0114e-02,  1.6149e-02, -1.0810e-01, -1.4679e-02,\n",
       "                       -4.2374e-02,  4.6734e-02,  1.3790e-02,  1.1481e-02, -4.2543e-02,\n",
       "                       -3.9067e-02,  5.1996e-02,  1.3028e-02, -1.2818e-02, -2.9937e-02,\n",
       "                        9.8027e-03,  2.3677e-02,  2.9081e-03, -3.9584e-02,  3.2506e-02,\n",
       "                       -4.6920e-02, -4.6307e-03, -6.4060e-02,  5.6148e-02,  8.9326e-02,\n",
       "                        1.9925e-02, -7.4379e-02,  1.1858e-02,  5.7722e-02, -1.0042e-02,\n",
       "                        7.0559e-02,  5.6305e-02, -2.5311e-02, -1.3332e-02, -5.5276e-02,\n",
       "                        1.1709e-02, -1.9650e-02,  1.5016e-02,  4.5067e-02, -9.5762e-02,\n",
       "                        5.3446e-02,  1.3248e-02, -7.4843e-03,  5.8443e-03,  8.2354e-02,\n",
       "                        3.0710e-02, -5.2153e-02,  1.4074e-02, -2.5234e-02,  1.4577e-02,\n",
       "                        4.4627e-02, -7.0625e-02, -3.1617e-02,  7.1680e-03,  2.4005e-02,\n",
       "                        5.3664e-02,  4.3401e-02, -4.8669e-03,  1.6664e-02, -7.3024e-02,\n",
       "                        2.7910e-02, -2.3077e-02,  7.3065e-02, -1.0560e-02,  7.2732e-03,\n",
       "                       -3.6421e-02, -1.0803e-02, -2.8137e-02,  9.7959e-03,  8.9886e-02,\n",
       "                       -3.5219e-02,  3.0298e-02, -7.9964e-03,  9.4145e-02,  2.5359e-02,\n",
       "                        3.0816e-02, -4.4642e-02,  7.2827e-02,  1.1100e-01, -4.0222e-02,\n",
       "                       -1.2540e-02, -2.2170e-02, -2.6560e-02,  7.1335e-03,  3.6662e-02,\n",
       "                        5.6060e-03,  3.4583e-02,  3.8872e-02, -4.2622e-02,  4.6095e-02,\n",
       "                       -6.3601e-02,  7.5797e-02,  3.3419e-02,  1.6867e-02,  1.7360e-02,\n",
       "                       -2.9089e-02,  4.1699e-02,  5.3621e-03,  7.2353e-03, -1.4156e-02,\n",
       "                        1.7366e-02,  1.0833e-02,  2.1025e-02, -3.1022e-02, -3.1829e-03,\n",
       "                       -4.2657e-03,  8.9750e-03,  3.4574e-02, -4.2802e-02,  1.9203e-02,\n",
       "                       -7.4180e-02, -2.0795e-02, -2.6585e-02, -3.3232e-02, -4.5616e-02,\n",
       "                        9.5546e-02,  4.4133e-02,  4.6472e-02,  3.8790e-02,  8.8782e-02,\n",
       "                       -2.6134e-02,  4.5010e-02, -3.1014e-02,  9.0169e-02, -2.0433e-02,\n",
       "                       -4.8684e-02, -1.3367e-02, -1.0403e-01, -8.4872e-03,  1.9542e-02,\n",
       "                        2.5251e-02, -9.6841e-03,  4.3187e-02,  4.0442e-04, -2.7602e-02,\n",
       "                       -8.5548e-02, -8.4101e-03, -1.0019e-03, -7.2041e-02, -8.3238e-02,\n",
       "                       -2.6230e-02, -6.5736e-02,  1.8271e-02, -4.9264e-02, -2.2640e-02,\n",
       "                       -7.0385e-02, -2.1463e-02, -6.9433e-03,  2.8793e-02, -5.8996e-02,\n",
       "                        4.0184e-02, -3.9434e-03, -5.6436e-03, -8.6915e-03, -1.4855e-02,\n",
       "                       -4.2223e-02,  2.4718e-03,  5.7906e-02, -6.6118e-02,  1.1307e-02,\n",
       "                        1.4124e-02,  8.7528e-02, -5.3368e-02, -6.7432e-02, -3.4481e-02,\n",
       "                        2.5024e-02,  8.9761e-03, -3.1820e-03, -4.1995e-02,  2.8039e-02,\n",
       "                       -2.9881e-02,  1.3175e-02,  4.2752e-03,  1.2111e-02, -2.8632e-02,\n",
       "                       -4.6412e-02, -2.3165e-02, -7.9306e-04,  8.5611e-02, -6.3786e-02,\n",
       "                       -2.0781e-02,  6.2646e-02,  2.8121e-02,  2.1558e-02,  6.8378e-03,\n",
       "                        2.2177e-02, -4.1966e-02,  5.2483e-02,  1.3181e-03,  6.8714e-02,\n",
       "                       -3.3997e-03,  3.5873e-02,  1.9057e-02,  4.4388e-02,  6.3833e-02,\n",
       "                       -3.1737e-02, -5.0145e-02,  6.9046e-03,  2.8982e-02,  1.5752e-02,\n",
       "                       -3.2221e-02, -3.6497e-02, -1.7512e-02, -1.2348e-02, -3.2372e-02,\n",
       "                       -4.4275e-02, -1.1811e-01,  3.6518e-02, -8.4541e-03, -3.9489e-02,\n",
       "                       -2.5928e-02,  2.4706e-02,  2.4725e-02,  3.6737e-02, -2.9941e-02,\n",
       "                        7.1677e-04,  3.3924e-02,  6.0114e-03,  1.9309e-02,  5.1816e-02,\n",
       "                       -4.6732e-02,  1.0059e-01, -2.4297e-02,  3.2495e-02,  3.1584e-02,\n",
       "                       -2.0686e-02,  8.1959e-03,  5.1143e-02,  3.1238e-03,  6.5646e-02,\n",
       "                       -2.9055e-03,  4.0308e-02, -6.5908e-02,  9.1267e-02,  6.5185e-03,\n",
       "                       -7.5546e-02,  8.5443e-04, -7.9018e-03, -1.9719e-02, -5.5298e-02,\n",
       "                       -5.5985e-02,  4.2173e-02, -3.1135e-02,  6.1821e-03, -5.8449e-02,\n",
       "                       -7.4279e-02, -3.8111e-02,  3.7818e-02,  1.0992e-01,  3.4663e-02,\n",
       "                        1.5368e-02,  4.1301e-02,  1.9832e-02, -3.1015e-02,  3.6532e-02,\n",
       "                       -2.4881e-02, -2.1762e-02, -1.2084e-02,  2.0953e-02, -7.2442e-02,\n",
       "                       -4.6323e-02,  3.6204e-03, -1.9837e-02, -5.2460e-02,  1.0005e-01,\n",
       "                       -7.2898e-02, -2.0519e-02, -5.2623e-02,  7.6583e-02, -2.0179e-02,\n",
       "                        2.1857e-02,  7.9043e-03, -2.7202e-02,  8.2623e-02, -2.7162e-02,\n",
       "                        9.7817e-03, -3.9657e-02, -8.5244e-03, -1.9964e-02, -6.1985e-02,\n",
       "                       -5.7112e-02,  7.8474e-03,  4.1693e-03,  1.0634e-02, -2.4503e-02,\n",
       "                       -4.8808e-02, -4.6813e-02,  6.3753e-02,  3.8440e-02, -5.9041e-02,\n",
       "                        4.8391e-04, -4.8131e-02, -3.4542e-02,  6.7729e-03,  1.4460e-02,\n",
       "                       -1.4773e-02,  7.4345e-03, -3.0319e-02, -4.0834e-02,  7.7954e-02,\n",
       "                        5.5941e-02, -2.9707e-02, -3.7024e-02, -5.0278e-02,  3.7614e-02,\n",
       "                       -7.3383e-02,  6.8514e-02, -9.0604e-03,  2.2350e-02,  4.1149e-02,\n",
       "                        9.2167e-03, -6.4851e-02, -9.1143e-03, -5.5650e-03, -2.1177e-03,\n",
       "                        8.0130e-02, -2.8893e-02,  9.8205e-02, -2.5417e-02,  1.2358e-02,\n",
       "                        6.0095e-03, -4.1563e-02,  1.8972e-02, -1.4523e-02,  3.8601e-02,\n",
       "                        7.6283e-02, -6.4471e-03, -7.4061e-03,  4.4588e-02,  3.9769e-02,\n",
       "                        2.1226e-02,  2.0118e-02, -8.3488e-02,  2.9410e-02,  6.4962e-02,\n",
       "                       -4.9911e-02,  2.3083e-02,  5.9690e-02,  3.3594e-02, -1.4132e-02,\n",
       "                        2.2304e-02, -3.2500e-02,  6.3168e-03,  2.9804e-04,  9.5704e-03,\n",
       "                       -7.6649e-02,  7.4968e-04,  5.0311e-03, -5.0196e-02,  9.4764e-02,\n",
       "                       -9.6009e-03, -3.4436e-02,  5.5316e-02, -1.1244e-01,  1.8494e-02,\n",
       "                        8.4056e-03, -7.1947e-02, -2.8846e-02,  1.4347e-03, -1.7969e-02,\n",
       "                        1.1474e-03, -1.1315e-02, -1.3326e-02,  2.9326e-02, -3.7306e-02,\n",
       "                        4.8790e-03, -4.0030e-02, -4.6670e-02, -7.5013e-02,  4.9544e-02,\n",
       "                       -8.4239e-03, -1.0321e-03,  7.4518e-02, -5.0817e-02,  2.0789e-02,\n",
       "                       -1.7150e-02, -3.6044e-02, -7.8318e-02, -3.9775e-02, -2.2535e-02,\n",
       "                       -4.2277e-02,  3.5785e-03, -3.6337e-02,  2.4370e-02, -7.3653e-02,\n",
       "                        5.7182e-03, -4.5578e-02, -8.2877e-02,  3.1017e-02, -1.4362e-02,\n",
       "                        2.3490e-03, -5.1804e-02, -4.3587e-02, -1.1218e-02,  6.0277e-02,\n",
       "                        2.2617e-02,  3.2674e-02, -3.3802e-02, -1.0739e-02, -3.3998e-02,\n",
       "                       -4.0368e-02, -2.3774e-02, -6.0151e-02, -8.0525e-02, -7.3242e-03,\n",
       "                        2.0661e-02,  6.7388e-02,  2.5408e-03,  2.6595e-02, -5.4658e-02,\n",
       "                        2.2755e-02, -6.7589e-02,  6.4671e-02, -2.9331e-02, -1.8461e-02,\n",
       "                        2.1199e-02,  3.0321e-02,  1.4939e-02, -4.5808e-02, -7.2654e-02,\n",
       "                        2.7363e-02, -9.1760e-04, -4.8657e-03,  2.9571e-02, -3.3194e-02,\n",
       "                        1.5901e-02, -5.4762e-02,  1.8560e-02,  4.3779e-02,  3.3733e-02,\n",
       "                       -1.4116e-03, -9.9320e-02,  3.7472e-02,  5.1822e-02,  7.2109e-02,\n",
       "                       -5.6968e-03,  2.0094e-02, -6.8672e-03, -5.2914e-02, -1.4161e-03,\n",
       "                        2.0089e-02, -3.0024e-03, -4.9861e-02, -9.1853e-02,  2.2921e-02,\n",
       "                        2.6733e-02, -7.0908e-04,  4.1733e-02,  2.4570e-02,  1.9224e-02,\n",
       "                       -1.9136e-03, -3.8150e-02, -7.1307e-02, -1.7834e-02, -3.4912e-02,\n",
       "                       -9.2533e-02, -2.8356e-02,  8.2270e-03,  7.0725e-02,  1.7760e-02,\n",
       "                        6.2729e-03, -5.3274e-02, -4.2679e-03, -1.9071e-02,  1.5275e-03,\n",
       "                        3.4477e-02, -5.9544e-02, -6.6846e-02,  1.0205e-01, -7.5236e-02,\n",
       "                        8.4140e-02, -3.7338e-02, -1.1169e-02, -9.6196e-03,  1.5439e-02,\n",
       "                       -3.1605e-02, -7.5027e-03,  4.6631e-03, -4.3785e-02,  2.1742e-02,\n",
       "                        8.3795e-03, -8.0654e-02, -2.6077e-02, -3.5546e-02,  3.4775e-02,\n",
       "                       -5.5459e-02, -5.0964e-02, -6.2931e-03,  2.3460e-03, -2.9502e-02,\n",
       "                        2.3012e-03,  2.0693e-02,  1.5095e-02,  3.8627e-02,  5.5807e-02,\n",
       "                        5.7819e-02, -3.0403e-02, -1.2626e-02, -3.5556e-02, -8.8739e-03,\n",
       "                        2.9190e-02,  7.6536e-02, -5.7467e-02, -1.1489e-04,  2.2304e-03,\n",
       "                        5.3971e-02,  1.3309e-02]])),\n",
       "             ('1.bias', tensor([-0.0344]))])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练好的权重参数\n",
    "model.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "348ef548",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-11-21T13:55:43.261683Z",
     "start_time": "2022-11-21T13:55:43.252707Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5911179173047473"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pytorch要自己写的东西, 比tensorflow多一些. \n",
    "# 比如计算准确率需要自己算.\n",
    "# 设定阈值, 认为>= 0.5是正样本\n",
    "((model(X).data.numpy() >= 0.6) == Y.numpy()).mean()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
